package com.zg.system.extra;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zg.common.core.R;
import com.zg.common.core.dao.system.SysLog;
import com.zg.common.core.dao.system.SysLogDao;
import com.zg.common.log.LogRequest;
import com.zg.common.log.OpTypeEnum;
import com.zg.system.extra.vo.LogSearchVo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Zxb
 */
@RestController
@RequiredArgsConstructor
public class LogController {
    private final SysLogDao logDao;

    @PostMapping("/log/del")
    @LogRequest(opModule = "日志管理", opName = "删除日志", opType = OpTypeEnum.DELETE)
    public R del(@RequestBody List<Long> ids) {
        logDao.removeBatchByIds(ids);
        return R.success();
    }

    @GetMapping("/log/page")
    @LogRequest(opModule = "日志管理", opName = "按分页查询日志", opType = OpTypeEnum.QUERY)
    public R page(LogSearchVo param) {
        IPage<SysLog> pageParam = new Page<>(param.getPage(), param.getPageSize());
        QueryWrapper<SysLog> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(param.getSearchStr())) {
            queryWrapper.lambda().like(SysLog::getSearchStr, "%" + param.getSearchStr() + "%");
        }
        if (StrUtil.isNotBlank(param.getOpModule())) {
            queryWrapper.lambda().like(SysLog::getOpModule, "%" + param.getOpModule() + "%");
        }
        if (StrUtil.isNotBlank(param.getOpName())) {
            queryWrapper.lambda().like(SysLog::getOpName, "%" + param.getOpName() + "%");
        }
        if (StrUtil.isNotBlank(param.getOpType())) {
            queryWrapper.lambda().eq(SysLog::getOpType, param.getOpType());
        }
        if (StrUtil.isNotBlank(param.getHttpMethod())) {
            queryWrapper.lambda().eq(SysLog::getHttpMethod, param.getHttpMethod());
        }
        if (StrUtil.isNotBlank(param.getState())) {
            queryWrapper.lambda().eq(SysLog::getState, param.getState());
        }
        if (StrUtil.isNotBlank(param.getRequestUri())) {
            queryWrapper.lambda().like(SysLog::getRequestUri, "%" + param.getRequestUri() + "%");
        }
        if (StrUtil.isNotBlank(param.getUsername())) {
            queryWrapper.lambda().like(SysLog::getUsername, "%" + param.getUsername() + "%");
        }
        if (StrUtil.isNotBlank(param.getSdate())) {
            queryWrapper.lambda().gt(SysLog::getRequestTime, DateUtil.parse(param.getSdate()));
        }
        if (StrUtil.isNotBlank(param.getEdate())) {
            queryWrapper.lambda().lt(SysLog::getRequestTime, DateUtil.offsetDay(DateUtil.parse(param.getEdate()), 1));
        }
        IPage<SysLog> result = logDao.page(pageParam, queryWrapper);
        return R.success(result);
    }
}
